
<?php
require_once 'application/models/UserRow.php';
class User extends Zend_Db_Table
{

	/**
	* The actual name of the table
	*
	* @var string
	*/
	protected $_name = 'user';
	
	/**
	* The table's primary key
	*
	* @var string
	*/
	protected $_primary = 'id';
	
	/**
	* The User class' row model
	*
	*/
	protected $_rowClass = 'UserRow';
	
	protected $_dependentTables = array('friends','Glocation'); 
	 
	/**
	* Retrieve a user using his email address
	*
	* @param string $email
	* @return UserRow
	*/
	function getUserByEmail($email)
	{
		$query = $this->select();
		$query->where('email = ?', $email);
		$result = $this->fetchRow($query);
		return $result;
	}
	
	/**
	* Retrieve user according to his gamer user_name
	*
	* @param string $user_name
	* @return UserRow
	*/
	function getUserByHandle($handle)
	{
		$query = $this->select();
		$query->where('user_name = ?', $handle);
		$result = $this->fetchRow($query);
		return $result;
	}
	
	/**
	* Retrieve user according to his gamer user_name
	*
	* @param string $user_name
	* @return UserRow
	*/
	function getUserByPhoneNumber($phoneNumber)
	{
		$query = $this->select();
		$query->where('phoneNumber = ?', $phoneNumber);
		$result = $this->fetchRow($query);
		return $result;
	}
	
	/**
	* Calculate the total number of registered users
	*
	* @return integer
	*/
	function getUserCount()
	{
		$query = $this->select('id');
		$result = $this->fetchAll($query);
		return count($result);
	}

	function getFriends()
	{
		$user = new User();
		$query = $user->select();
		$query->from(array('u' => 'users'),
		array('u.id', 'u.user_name', 'u.first_name', 'u.last_name'));
		$query->join(array('f' => 'friends'), 'f.friend_id = u.id', array());
		$query->where('f.user_id = ?', $this->id);
		$results = $user->fetchAll($query);
		return $results;
	}	
	
}
?>